<?php

!defined('IN_NOVA') && exit('Access Denied!');

class XKLog extends Application {

	function __construct() {
		global $cache,$db;
		parent::__construct();

		// 初始化缓存
		//$cache_list = array( 'config', 'category', 'count', 'tag' );
		if( $cache->config == NULL ) {
			$content = array();
			$content = $db->fetch_all( 'SELECT * FROM `' . DB_PREFIX . 'set' );
			$cache->set( 'config', $content[0], 0 );
		}
		if( $cache->menu == NULL ) {
			$content = array();
			$query = $db->query( 'SELECT name,title,type,address FROM `' . DB_PREFIX . 'menu` ORDER BY num Asc,mid Desc' );
			while ( $row = $db->fetch_array( $query ) ) {
				$content[$row['name']] = array( 'title' => $row['title'], 'type' => $row['type'], 'address' => $row['address'] );
			}
			$cache->set( 'menu', $content, 0 );
			unset( $query, $row );
		}
		if( $cache->tag == NULL ) {
			$content = array();
			$query = $db->query( 'SELECT * FROM `' . DB_PREFIX . 'tags` ORDER BY t_id Asc' );
			while ( $row = $db->fetch_array( $query ) ) {
				$content[$row['t_id']] = array( 't_name' => $row['t_name'], 't_num' => $row['t_num'] );
			}
			$cache->set( 'tag', $content, 0 );
			unset( $query, $row );
		}
		if( $cache->category == NULL ) {
			$content = array();
			$query = $db->query( 'SELECT cid,pid,name,alias,articlenum,description FROM `' . DB_PREFIX . 'category` ORDER BY num ASC,cid DESC' );
			while ( $row = $db->fetch_array( $query ) ){
				$content[$row['cid']] = $row;
			}
			$cache->set( 'category', $content, 0 );
			unset( $query, $row );
		}
		if( $cache->count == NULL ) {
			$content = array(
				'article_num' => $db->result( 'SELECT count(id) FROM `' . DB_PREFIX . 'article` WHERE isdel=False' ),
				'comment_num' => $db->result( 'SELECT count(cid) FROM `' . DB_PREFIX . 'comment` WHERE isdel=False AND articleid<>0' ),
				'guestbook_num' => $db->result( 'SELECT count(cid) FROM `' . DB_PREFIX . 'comment` WHERE isdel=False AND articleid=0' ),
				'laodao_num' => $db->result( 'SELECT count(ld_id) FROM `' . DB_PREFIX . 'laodao`' ),
				'guestbook_num_censor' => $db->result( "SELECT count(cid) FROM `" . DB_PREFIX . "comment` WHERE isshow=0 and isdel=0 and articleid=0" ),
				'comment_num_censor' => $db->result( "SELECT count(cid) FROM `" . DB_PREFIX . "comment` WHERE isshow=0 and isdel=0 and articleid<>0" ),
				'file_num' => $db->result( "SELECT count(ul_id) FROM `" . DB_PREFIX . "upload`" ),
			);
			$cache->set( 'count', $content, 0 );
		}
		if( $cache->style == NULL ) {
			$styles = array();
			$result = read_dir( APP_THEME );
			foreach ($result as $each_dir) {
				$each_dir = str_replace( APP_THEME, '', $each_dir );
				if( !Theme::is_legal( $each_dir ) ) continue;
				$styles[$each_dir]['directory'] = $each_dir;
				$styles[$each_dir]['path'] = THEME_PATH . $each_dir . '/';
				$infofile = APP_THEME . $each_dir . '/info.xml';
				$info = Theme::parse_xml_info( $infofile, 'style' );
				$styles[$each_dir] = array_merge( $styles[$each_dir], $info );
			}
			$cache->style = $styles;
			unset( $styles );
		}
		if( $cache->plugin == NULL ) {
			$plugins = array();
			$result = read_dir( APP_PLUGIN );
			foreach ($result as $each_dir) {
				$each_dir = str_replace( APP_PLUGIN, '', $each_dir );
				if( !Plugin::is_legal( $each_dir ) ) continue;
				if( !Plugin::check_install( PLUGIN_PATH . $each_dir . '/' ) ) continue;
				$plugins[$each_dir]['directory'] = $each_dir;
				$plugins[$each_dir]['path'] = PLUGIN_PATH . $each_dir . '/';
				$infofile = APP_PLUGIN . $each_dir . 'info.xml';
				$info = Plugin::parse_xml_info( $infofile, 'plugin' );
				$plugins[$each_dir] = array_merge( $plugins[$each_dir], $info );
			}
			$cache->plugin = $plugins;
			unset( $plugins );
		}
		unset( $content );
	}

	public function init() {
		global $request,$cache,$theme,$user;

		// 设置博客风格
		$style = $this->C( APP_PREFIX . 'style' );
		if( $style != NULL && array_key_exists( $style , $cache->style ) ) {
			$theme->style = $style;
		}
		$style = $this->G( 'p' );
		if( is_string( $style ) && array_key_exists( $style , $cache->style ) ) {
			$theme->style = $style;
			$app_path = str_replace( ' ','%20', APP_PATH );
			setcookie( APP_PREFIX . 'style', $style, PHP_TIME + 31536000, $app_path );
			unset( $app_path );
		}
		unset( $style );

		// 判断是否开放博客
		if( $cache->config['enable'] == 0 ) {
			$theme->show_message( $cache->config['closereason'] );
			return;
		}

		// 尝试自动登录
		/*$username = $request->get( APP_PREFIX . 'username', 'C' );
		$password = $request->get( APP_PREFIX . 'password', 'C' );
		if( $username != NULL && $password != NULL ){
			if( $user->login( $username, $password, -1 ) != 1 ) {
				setcookie( APP_PREFIX . 'username', NULL );
				setcookie( APP_PREFIX . 'password', NULL );
			}
		}
		unset( $username, $password );*/

		// 判断在线人数是否达到上限
		/*if( $user->online() > $cache->get( 'config', 'maxonline' ) ){
			$theme->show_message( L('_TOO_MANY_ONLINE_GUESTS_') );
			return;
		}*/

		$this->page();
	}

	// 页面
	private function page( $page_make = FALSE ) {
		global $theme,$request;
		//[DEBUG]
		global $log;
		//[/DEBUG]

		// 防止非法的文件包含
		$tmparray = array('/',"\0",'..');
		if( str_replace( $tmparray, '', $this->m ) != $this->m ) {
			$theme->err_404();
			return;
		}

		$theme->module = $this->m;
		$theme->page_make = $page_make;

		//[DEBUG]
		$runtime = microtime( TRUE );
		//[/DEBUG]

		if( is_file( APP_MODE . $this->m . '.class.php' ) ) {
			include( APP_MODE . $this->m . '.class.php' );

			$name = ucwords( $this->m );
			$page = new $name();

			if( $this->p{0} != '_' && method_exists( $page, $this->p ) ) {
				$page->{$this->p}();
			} else {
				if( method_exists( $page, 'start' ) ) {
					$page->start();
				} else {
					$theme->err_404();
					return;
				}
			}
		} else {
			include( APP_MODE . 'page.class.php' );

			$page = new Page();

			$page->name = $this->m;
			$page->start();
		}

		//[DEBUG]
		$runtime = microtime( TRUE ) - $runtime;
		if( defined( 'APP_DEBUG' ) && APP_DEBUG === TRUE ) {
			$log->add( '页面处理用时：' . number_format( $runtime, 4 ) * 1000 . 'ms。', E_USER_NOTICE );
		}
		//[/DEBUG]
	}

}
?>
